﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.UnifyShow.Business;
using Microsoft.Practices.Unity;
using Wicresoft.UnifyShow.BusinessImpl;
using System.Windows.Forms.DataVisualization.Charting;
using System.IO;
using System.Drawing;
using Wicresoft.UnifyShow.Portal.Extensions;

namespace Wicresoft.UnifyShow.Portal.Controllers
{
    public class ChangeController : BaseController
    {
        private IChange ichange;

        protected override void Dispose(bool disposing)
        {
            ((IDisposable)ichange).Dispose();
        }

        [InjectionConstructor]
        public ChangeController(IChange e)
        {
            ichange = e;
        }
        //超时的变更数量/百分比
        [OutputCache(CacheProfile = "Report", VaryByParam = "sid;bnid;sTime;eTime")]
        public ActionResult TimeOut(int? sid, int? bnid, string sTime, string eTime)
        {
            DateTime sfTime = Convert.ToDateTime(sTime);
            DateTime efTime = Convert.ToDateTime(eTime);
            decimal allchcount = ichange.GetclosedChangeCountBySiteAndBusiness(sid, bnid, sfTime, efTime);
            decimal timeOutchcount = ichange.GetChangeResolvedTimeOutCountBySiteAndBusiness(sid, bnid, sfTime, efTime);

            double chTimeOut = (double)timeOutchcount / (double)allchcount;
            double chTimeIn = 1 - chTimeOut;
            Chart chart = new Chart();
            chart.Width = GlobalHelper.Width;
            chart.Height = GlobalHelper.Height;

            double[] yval = { chTimeOut, chTimeIn };
            string[] xval = { (chTimeOut * 100).ToString("0.00") + "%", (chTimeIn * 100).ToString("0.00") + "%" };

            Series series = new Series();
            series.ChartType = SeriesChartType.Pie;

            series.Points.DataBindXY(xval, yval);

            series.Points[0].LegendText = "超时的变更 ";
            series.Points[1].LegendText = "未超时的变更 ";

            Legend l = new Legend();
            l.BorderColor = Color.Black;
            ChartArea chartArea = new ChartArea();
            chartArea.AxisY.LabelStyle.Format = "0%";
            //Title t = new Title("超时的变更数量");
            //t.Font = GlobalHelper.fontTitle;
            //chart.Titles.Add(t);
            chart.Legends.Add(l);
            chart.Series.Add(series);
            chart.ChartAreas.Add(chartArea);

            //--chart样式--
            chart.Legends[0].Font = GlobalHelper.fontLegend;
            //chart1.Legends[0].FontColorCombo = Color.Red;
            //设置图颜色
            //chart1.Palette = ChartColorPalette.Pastel;
            //此属性设置为None才可以自定义调色板
            chart.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.None;
            // 自定义调色板
            Color[] colorSet = GlobalHelper.TwoSetPei;
            chart.PaletteCustomColors = colorSet;
            //图样式 concave/SoftEdge
            series["PieDrawingStyle"] = "SoftEdge";
            //分割饼图
            //series.Points[0]["Exploded"] = "true";
            // 设置标签风格
            series["PieLabelStyle"] = "Inside";
            return new ChartResult(chart);
        }
        //变更总数
        [OutputCache(CacheProfile = "Report", VaryByParam = "sid;bnid;sTime;eTime")]
        public ActionResult ChangeCount(int? sid, int? bnid, string sTime, string eTime)
        {
            //数据处理
            DateTime sfTime = DateTime.Parse(sTime.ToString().Trim());
            int year1 = Convert.ToInt16(sfTime.ToString("yyyy"));
            int month1 = Convert.ToInt16(sfTime.ToString("MM"));
            int day1 = Convert.ToInt16(sfTime.ToString("dd"));
            DateTime efTime = DateTime.Parse(eTime.ToString().Trim());
            int year2 = Convert.ToInt16(efTime.ToString("yyyy"));
            int month2 = Convert.ToInt16(efTime.ToString("MM"));
            int day2 = Convert.ToInt16(efTime.ToString("dd"));

            List<IncidentAndChangeModel> cms = ichange.GetChangeCountGroupTimeBySiteAndBusiness(sid, bnid, sfTime, efTime);
            Series series1 = new Series();
            Chart colChart = new Chart();
            ChartArea chartArea1 = new ChartArea();
            //var group = cms.GroupBy(b => b.sign);
            //foreach (var k in group)
            //{
            //    series1.Points.AddXY(k.Key.ToString().Substring(k.Key.ToString().Length - 2), k.Average(b => b.alls));
            //}
            for (DateTime dt = new DateTime(year1, month1, day1); dt < new DateTime(year2, month2, day2); dt = dt.AddMonths(1))
            {
                //截取年月
                string mo = dt.Year.ToString() + dt.Month.ToString();
                if (dt.Month.ToString().Length < 2)
                {
                    mo = dt.Year.ToString() + "0" + dt.Month.ToString();
                }
                string b = mo.Substring(0, 4);
                var item1 = cms.Where(p => p.sign == mo).FirstOrDefault();
                if (item1 != null)
                {
                    series1.Points.AddXY(mo.Substring(mo.Length - 2), item1.alls);
                }
                else
                {
                    series1.Points.AddXY(mo.Substring(mo.Length - 2), 0);
                }
            }

            //设置Y轴刻度间隔
            double max = 0;
            double min = 0;
            if (series1.Points.Count != 0)
            {
                max = Math.Floor(series1.Points.FindMaxByValue().YValues[0] * 1.1);
                min = Math.Floor(series1.Points.FindMinByValue().YValues[0] * 0.9);
            }
            double differ = (max - min) / 10.0;
            int intervalY = (int)Math.Floor(differ) + 1;
            chartArea1.AxisY.Maximum = max;
            chartArea1.AxisY.Minimum = min;
            chartArea1.AxisY.Interval = intervalY;
            chartArea1.AxisX.Title = "(单位/月)";
            //chartArea1.AxisY.Title = "(单位/个)";
            series1.BorderColor = System.Drawing.Color.FromArgb(180, 26, 59, 105);
            series1.IsValueShownAsLabel = true;
            series1.XValueType = ChartValueType.String;
            ////设置宽度
            series1["PointWidth"] = "0.6";
            series1["DrawingStyle"] = "Emboss";
            series1.Name = "个数";
            series1.ChartType = SeriesChartType.Column;
            series1.IsValueShownAsLabel = true;
            //设置墙的宽度为
            chartArea1.Area3DStyle.WallWidth = 30;
            //设置XY轴刻度间隔
            chartArea1.AxisX.Interval = 1;
            //是否3D显示
            //chartArea1.Area3DStyle.Enable3D = true;
            //设置图列
            Legend x = new Legend("Legend1");
            x.BorderColor = Color.Black;
            colChart.Legends.Add(x);
            //--chart样式--
            colChart.Legends[0].Font = GlobalHelper.fontLegend;
            //设置图标题（上方显示）
            //Title t = new Title("变更总数");
            //t.Font = GlobalHelper.fontTitle;
            //colChart.Titles.Add(t);

            colChart.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.None;
            Color[] colorSet = GlobalHelper.OneSet;
            colChart.PaletteCustomColors = colorSet;
            //不显示网格线
            chartArea1.AxisX.MajorGrid.Enabled = chartArea1.AxisY.MajorGrid.Enabled = false;
            colChart.Size = new System.Drawing.Size(GlobalHelper.Width, GlobalHelper.Height);
            colChart.Series.Add(series1);
            colChart.ChartAreas.Add(chartArea1);
            return new ChartResult(colChart);
        }
        //变更实施结果分类的数量/百分比
        [OutputCache(CacheProfile = "Report", VaryByParam = "sid;bnid;sTime;eTime")]
        public ActionResult ResultSort(int? sid, int? bnid, string sTime, string eTime)
        {
            DateTime sfTime = Convert.ToDateTime(sTime);
            DateTime efTime = Convert.ToDateTime(eTime);
            List<IncidentAndChangeModel> ResultSorts = ichange.GetChangeGroupResultBySiteAndBusiness(sid, bnid, sfTime, efTime);
            if (ResultSorts.FirstOrDefault(p => p.ID == 0) == null)
            {
                ResultSorts.Add(new IncidentAndChangeModel { alls = 0, ID = 0 });
            }
            if (ResultSorts.FirstOrDefault(p => p.ID == 1) == null)
            {
                ResultSorts.Add(new IncidentAndChangeModel { alls = 0, ID = 1 });
            }
            if (ResultSorts.FirstOrDefault(p => p.ID == 2) == null)
            {
                ResultSorts.Add(new IncidentAndChangeModel { alls = 0, ID = 2 });
            }
            if (ResultSorts.FirstOrDefault(p => p.ID == 3) == null)
            {
                ResultSorts.Add(new IncidentAndChangeModel { alls = 0, ID = 3 });
            }
            if (ResultSorts.FirstOrDefault(p => p.ID == 4) == null)
            {
                ResultSorts.Add(new IncidentAndChangeModel { alls = 0, ID = 4 });
            }

            List<double> lyval = new List<double>();
            List<string> lxval = new List<string>();
            List<string> llegend = new List<string>();

            double sum = (double)ResultSorts.Sum(p => p.alls);
            foreach (var ResultSort in ResultSorts)
            {
                double percent = (double)ResultSort.alls / sum;
                lyval.Add(percent);
                lxval.Add((percent * 100).ToString("0.00") + "%");
                switch (ResultSort.ID.ToString())
                {
                    case "0": llegend.Add("完全成功实施");
                        break;
                    case "1": llegend.Add("成功实施，但存在问题");
                        break;
                    case "2": llegend.Add("回滚成功");
                        break;
                    case "3": llegend.Add("回滚不成功");
                        break;
                    case "4": llegend.Add("变更撤销");
                        break;
                }
            }

            Chart chart = new Chart();
            chart.Width = GlobalHelper.Width;
            chart.Height = GlobalHelper.Height;

            double[] yval = lyval.ToArray();
            string[] xval = lxval.ToArray();
            string[] legend = llegend.ToArray();

            Series series = new Series();
            series.ChartType = SeriesChartType.Pie;

            series.Points.DataBindXY(xval, yval);

            for (int i = 0; i < legend.Length; i++)
            {
                series.Points[i].LegendText = legend[i];
            }

            Legend l = new Legend();
            l.BorderColor = Color.Black;
            ChartArea chartArea = new ChartArea();
            chartArea.AxisY.LabelStyle.Format = "0%";
            //Title t = new Title("变更实施结果分类的数量");
            //t.Font = GlobalHelper.fontTitle;

            chart.Legends.Add(l);
            //--chart样式--
            chart.Legends[0].Font = GlobalHelper.fontLegend;
            //chart.Titles.Add(t);
            chart.Series.Add(series);
            chart.ChartAreas.Add(chartArea);

            //设置图颜色
            //chart1.Palette = ChartColorPalette.Pastel;
            //此属性设置为None才可以自定义调色板
            chart.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.None;
            // 自定义调色板
            Color[] colorSet = GlobalHelper.SixColorSet;
            chart.PaletteCustomColors = colorSet;
            //图样式 concave/SoftEdge
            series["PieDrawingStyle"] = "SoftEdge";
            //分割饼图
            //series.Points[0]["Exploded"] = "true";
            // 设置标签风格
            series["PieLabelStyle"] = "Inside";
            return new ChartResult(chart);
        }
        //重大风险&高风险的变更数量/百分比
        [OutputCache(CacheProfile = "Report", VaryByParam = "sid;bnid;sTime;eTime")]
        public ActionResult ImpactSort(int? sid, int? bnid, string sTime, string eTime)
        {
            //数据处理
            DateTime sfdate = DateTime.Parse(sTime.ToString().Trim());
            int sfyear = Convert.ToInt16(sfdate.ToString("yyyy"));
            int sfmonth = Convert.ToInt16(sfdate.ToString("MM"));
            int sfday = Convert.ToInt16(sfdate.ToString("dd"));
            DateTime efdate = DateTime.Parse(eTime.ToString().Trim());
            int efyear = Convert.ToInt16(efdate.ToString("yyyy"));
            int efmonth = Convert.ToInt16(efdate.ToString("MM"));
            int efday = Convert.ToInt16(efdate.ToString("dd"));

            List<IncidentAndChangeModel> allchcount = ichange.GetChangeCountGroupTimeBySiteAndBusiness(sid, bnid, sfdate, efdate);
            List<IncidentAndChangeModel> impactchcount = ichange.GetChangeImpactSortCountBySiteAndBusiness(sid, bnid, sfdate, efdate);


            Series series1 = new Series();
            Series series2 = new Series();
            Chart colChart = new Chart();
            ChartArea chartArea1 = new ChartArea();

            for (DateTime dt = new DateTime(sfyear, sfmonth, sfday); dt < new DateTime(efyear, efmonth, efday); dt = dt.AddMonths(1))
            {
                //截取年月
                string mo = dt.Year.ToString() + dt.Month.ToString();
                if (dt.Month.ToString().Length < 2)
                {
                    mo = dt.Year.ToString() + "0" + dt.Month.ToString();
                }
                string b = mo.Substring(0, 4);
                //严重
                var item = impactchcount.Where(p => p.sign == mo).FirstOrDefault();
                var allitem = allchcount.Where(p => p.sign == mo).FirstOrDefault();

                //百分比
                if (allitem != null && item != null)
                {
                    Decimal g1 = Convert.ToDecimal(item.alls / allitem.alls);
                    series2.Points.AddXY(mo.Substring(mo.Length - 2), g1.ToString("0.00"));
                    series1.Points.AddXY(mo.Substring(mo.Length - 2), (1 - g1).ToString("0.00"));
                }
                else
                {
                    series2.Points.AddXY(mo.Substring(mo.Length - 2), 0);
                    series1.Points.AddXY(mo.Substring(mo.Length - 2), 0);
                }
            }
            chartArea1.AxisX.Title = "(单位/月)";
            //chartArea1.AxisY.Title = "(单位/个)";
            chartArea1.Name = "Default";
            chartArea1.AxisY.LabelStyle.Format = "0%";
            //设置图标题（下方显示）
            //chartArea1.AxisX.Title = "事件影响程度";
            //是否3D显示
            chartArea1.Area3DStyle.Enable3D = true;
            //设置图列
            Legend x = new Legend("Legend1");
            //x.BackColor = Color.White;
            x.BorderColor = Color.Black;
            //x.Title = "影响程度";
            colChart.Legends.Add(x);
            //--chart样式--
            colChart.Legends[0].Font = GlobalHelper.fontLegend;
            //设置图标题（上方显示）
            //Title t = new Title("重大风险&高风险的变更数量");
            //t.Font = GlobalHelper.fontTitle;
            //colChart.Titles.Add(t);
            //设置XY轴刻度间隔
            chartArea1.AxisY.Interval = 0.2;
            chartArea1.AxisX.Interval = 1;
            //设置XY轴最大值
            chartArea1.AxisY.Maximum = 1;
            //chartArea1.AxisX.Maximum = 12;
            //设置宽度
            series1["PointWidth"] = "0.5";
            series2["PointWidth"] = "0.5";
            //设置墙的宽度为
            chartArea1.Area3DStyle.WallWidth = 2;
            //设置背景为白色
            chartArea1.BackColor = Color.White;


            //series1.BorderColor = System.Drawing.Color.FromArgb(189, 26, 59, 115);
            //series1.ChartArea = "Default";
            //series1.Legend = "Default";
            series2.Name = "重大";
            series1.Name = "一般";
            //series1.XValueType = ChartValueType.String;
            series1.LabelFormat = "0%";
            series2.LabelFormat = "0%";
            series1.ChartType = SeriesChartType.StackedColumn;
            series2.ChartType = SeriesChartType.StackedColumn;
            series1.IsValueShownAsLabel = true;
            series2.IsValueShownAsLabel = true;

            colChart.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.None;
            Color[] colorSet = GlobalHelper.TwoSetBar;
            colChart.PaletteCustomColors = colorSet;

            colChart.Size = new System.Drawing.Size(GlobalHelper.Width, GlobalHelper.Height);
            colChart.Series.Add(series1);
            colChart.Series.Add(series2);
            colChart.ChartAreas.Add(chartArea1);
            return new ChartResult(colChart);
        }
        //变更实施成功的超时数量/百分比
        [OutputCache(CacheProfile = "Report", VaryByParam = "sid;bnid;sTime;eTime")]
        public ActionResult SuccessResultSort(int? sid, int? bnid, string sTime, string eTime)
        {
            DateTime sfTime = Convert.ToDateTime(sTime);
            DateTime efTime = Convert.ToDateTime(eTime);
            List<ChangeModel> allSuccessch = ichange.GetSuccessChangeBySiteAndBusiness(sid, bnid, sfTime, efTime);
            List<ChangeModel> timeOutSuccessch = allSuccessch.Where(p => p.EndTime > p.PlanEndTime).ToList();


            double chTimeOut = allSuccessch.Count == 0 ? 0 : (double)timeOutSuccessch.Count() / (double)allSuccessch.Count();
            double chTimeIn = allSuccessch.Count == 0 ? 0 : 1 - chTimeOut;

            Chart chart = new Chart();
            chart.Width = GlobalHelper.Width;
            chart.Height = GlobalHelper.Height;

            double[] yval = { chTimeOut, chTimeIn };
            string[] xval = { (chTimeOut * 100).ToString("0.00") + "%", (chTimeIn * 100).ToString("0.00") + "%" };

            Series series = new Series();
            series.ChartType = SeriesChartType.Pie;

            series.Points.DataBindXY(xval, yval);

            series.Points[0].LegendText = "超时的变更 ";
            series.Points[1].LegendText = "未超时的变更 ";

            Legend l = new Legend();
            l.BorderColor = Color.Black;
            ChartArea chartArea = new ChartArea();
            chartArea.AxisY.LabelStyle.Format = "0%";
            //Title t = new Title("变更实施成功的超时数量");
            //t.Font = GlobalHelper.fontTitle;

            chart.Legends.Add(l);
            //--chart样式--
            chart.Legends[0].Font = GlobalHelper.fontLegend;
            //chart.Titles.Add(t);
            chart.Series.Add(series);
            chart.ChartAreas.Add(chartArea);
            //设置图颜色
            //chart1.Palette = ChartColorPalette.Pastel;
            //此属性设置为None才可以自定义调色板
            chart.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.None;
            // 自定义调色板
            Color[] colorSet = GlobalHelper.TwoSetPei;
            chart.PaletteCustomColors = colorSet;
            //图样式 concave/SoftEdge
            series["PieDrawingStyle"] = "SoftEdge";
            //分割饼图
            //series.Points[0]["Exploded"] = "true";
            // 设置标签风格
            series["PieLabelStyle"] = "Inside";
            return new ChartResult(chart);
        }
        //变更平均完成时间
        [OutputCache(CacheProfile = "Report", VaryByParam = "sid;bnid;sTime;eTime")]
        public ActionResult AvgTime(int? sid, int? bnid, string sTime, string eTime)
        {
            //数据处理
            DateTime date1 = DateTime.Parse(sTime.ToString().Trim());
            int year1 = Convert.ToInt16(date1.ToString("yyyy"));
            int month1 = Convert.ToInt16(date1.ToString("MM"));
            int day1 = Convert.ToInt16(date1.ToString("dd"));
            DateTime date2 = DateTime.Parse(eTime.ToString().Trim());
            int year2 = Convert.ToInt16(date2.ToString("yyyy"));
            int month2 = Convert.ToInt16(date2.ToString("MM"));
            int day2 = Convert.ToInt16(date2.ToString("dd"));
            //取数据
            List<IncidentAndChangeModel> ims = ichange.GetAvgtimeChangeBySiteAndBusiness(sid, bnid, date1, date2);
            Series series1 = new Series();
            Chart colChart = new Chart();
            ChartArea chartArea1 = new ChartArea();
            for (DateTime dt = new DateTime(year1, month1, day1); dt < new DateTime(year2, month2, day2); dt = dt.AddMonths(1))
            {
                //截取年月
                string mo = dt.Year.ToString() + dt.Month.ToString();
                if (dt.Month.ToString().Length < 2)
                {
                    mo = dt.Year.ToString() + "0" + dt.Month.ToString();
                }
                string b = mo.Substring(0, 4);
                var item1 = ims.Where(p => p.sign == mo).FirstOrDefault();
                if (item1 != null)
                {
                    series1.Points.AddXY(mo.Substring(mo.Length - 2), item1.alls.ToString("0.00"));
                }
                else
                {
                    series1.Points.AddXY(mo.Substring(mo.Length - 2), 0);
                }
            }

            //设置Y轴刻度间隔
            double max = 0;
            double min = 0;
            if (series1.Points.Count != 0)
            {
                max = series1.Points.FindMaxByValue().YValues[0] * 1.5;
                min = series1.Points.FindMinByValue().YValues[0] * 1.2;
            }
            double differ = (max - min) / 10.0;
            int intervalY = (int)Math.Floor(differ);
            chartArea1.AxisY.Maximum = max;
            chartArea1.AxisY.Minimum = min;
            chartArea1.AxisY.Interval = intervalY;
            chartArea1.AxisX.Title = "(单位/月)";
            //chartArea1.AxisY.Title = "(单位/个)";
            series1.BorderColor = System.Drawing.Color.FromArgb(180, 26, 59, 105);
            series1.IsValueShownAsLabel = true;
            series1.XValueType = ChartValueType.String;
            ////设置宽度
            series1["PointWidth"] = "0.6";
            series1["DrawingStyle"] = "Emboss";
            series1.Name = "小时";
            series1.ChartType = SeriesChartType.Column;
            series1.IsValueShownAsLabel = true;
            //设置墙的宽度为
            chartArea1.Area3DStyle.WallWidth = 30;
            //设置XY轴刻度间隔
            chartArea1.AxisX.Interval = 1;
            //是否3D显示
            //chartArea1.Area3DStyle.Enable3D = true;
            //不显示网格线
            chartArea1.AxisX.MajorGrid.Enabled = chartArea1.AxisY.MajorGrid.Enabled = false;
            //设置图列
            Legend x = new Legend("Legend1");
            x.BorderColor = Color.Black;
            colChart.Legends.Add(x);
            //--chart样式--
            colChart.Legends[0].Font = GlobalHelper.fontLegend;
            //设置图标题（上方显示）
            //Title t = new Title("变更平均完成时间");
            //t.Font = GlobalHelper.fontTitle;
            //colChart.Titles.Add(t);

            colChart.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.None;
            Color[] colorSet = GlobalHelper.OneSet;
            colChart.PaletteCustomColors = colorSet;
            colChart.Size = new System.Drawing.Size(GlobalHelper.Width, GlobalHelper.Height);
            colChart.Series.Add(series1);
            colChart.ChartAreas.Add(chartArea1);
            return new ChartResult(colChart);
        }
        //来自其他工单类型的变更数量
        [OutputCache(CacheProfile = "Report", VaryByParam = "sid;bnid;sTime;eTime")]
        public ActionResult EscalationChange(int? sid, int? bnid, string sTime, string eTime)
        {

            Chart chart = new Chart();
            Series series1 = new Series();
            DateTime sfTime = Convert.ToDateTime(sTime);
            DateTime efTime = Convert.ToDateTime(eTime);
            List<IncidentAndChangeModel> icm = ichange.GetEscalationChangeCountBySiteAndBusiness(sid, bnid, sfTime, efTime);
            if (icm.FirstOrDefault(p => p.ID == 1000) == null)
            {
                icm.Add(new IncidentAndChangeModel { alls = 0, ID = 1000 });
            }
            if (icm.FirstOrDefault(p => p.ID == 2000) == null)
            {
                icm.Add(new IncidentAndChangeModel { alls = 0, ID = 2000 });
            }
            if (icm.FirstOrDefault(p => p.ID == 3000) == null)
            {
                icm.Add(new IncidentAndChangeModel { alls = 0, ID = 3000 });
            }
            if (icm.FirstOrDefault(p => p.ID == 4000) == null)
            {
                icm.Add(new IncidentAndChangeModel { alls = 0, ID = 4000 });
            }
            if (icm.FirstOrDefault(p => p.ID == 5000) == null)
            {
                icm.Add(new IncidentAndChangeModel { alls = 0, ID = 5000 });
            }
            if (icm.FirstOrDefault(p => p.ID == 6000) == null)
            {
                icm.Add(new IncidentAndChangeModel { alls = 0, ID = 6000 });
            }

            double otherNum = 0;
            double changeNum = 0;
            for (int i = 0; i < icm.Count(); i++)
            {
                if (icm[i].ID == 1000)
                {
                    otherNum += Convert.ToDouble(icm[i].alls);
                }
                if (icm[i].ID == 5000)
                {
                    otherNum += Convert.ToDouble(icm[i].alls);
                }
                if (icm[i].ID == 2000 ||icm[i].ID == 3000 || icm[i].ID == 4000 ||icm[i].ID == 6000 || icm[i].ID == 0)
                {
                    changeNum += Convert.ToDouble(icm[i].alls);
                }
            }
            double incidentNums = Convert.ToDouble(otherNum / (otherNum + changeNum));
            double otherNums = Convert.ToDouble((changeNum / (otherNum + changeNum)));
            double[] yval = { incidentNums, otherNums };
            string[] xval = { (incidentNums * 100).ToString("0.00") + "%", (otherNums * 100).ToString("0.00") + "%" };
            series1.Points.DataBindXY(xval, yval);
            series1.ChartType = SeriesChartType.Pie;
            series1.Points[0].LegendText = "来自其他工单类型的变更";
            series1.Points[1].LegendText = "普通变更";
            //List<double> lyval = new List<double>();
            //List<string> lxval = new List<string>();
            //List<string> llegend = new List<string>();
            //double sum = (double)icm.Sum(p => p.alls);
            //foreach (var ResultSort in icm)
            //{
            //    double percent = (double)ResultSort.alls / sum;
            //    lyval.Add(percent);
            //    lxval.Add((percent * 100).ToString("0.00") + "%");
            //    switch (ResultSort.ID.ToString())
            //    {
            //        case "1000": llegend.Add("问题管理");
            //            break;
            //        case "2000": llegend.Add("代维公司");
            //            break;
            //        case "3000": llegend.Add("产品供应商");
            //            break;
            //        case "4000": llegend.Add("按计划");
            //            break;
            //        case "5000": llegend.Add("重大事件");
            //            break;
            //        case "6000": llegend.Add("用户需求");
            //            break;
            //    }
            //}
            ////Series
            //double[] yval = lyval.ToArray();
            //string[] xval = lxval.ToArray();
            //string[] legend = llegend.ToArray();
            //series1.ChartType = SeriesChartType.Pie;
            //series1.Points.DataBindXY(xval, yval);
            //for (int i = 0; i < legend.Length; i++)
            //{
            //    series1.Points[i].LegendText = legend[i];
            //}
            chart.Width = GlobalHelper.Width;
            chart.Height = GlobalHelper.Height;

            Legend l = new Legend();
            l.BorderColor = Color.Black;
            ChartArea chartArea = new ChartArea();
            chartArea.AxisY.LabelStyle.Format = "0%";
            //Title t = new Title("来自其他工单类型变更数量");
            //t.Font = GlobalHelper.fontTitle;

            chart.Legends.Add(l);
            //chart.Titles.Add(t);
            chart.Series.Add(series1);
            chart.ChartAreas.Add(chartArea);
            //--chart样式--
            chart.Legends[0].Font = GlobalHelper.fontLegend;
            //chart1.Legends[0].FontColorCombo = Color.Red;
            //设置图颜色
            //chart1.Palette = ChartColorPalette.Pastel;
            //此属性设置为None才可以自定义调色板
            chart.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.None;
            // 自定义调色板
            Color[] colorSet = GlobalHelper.SixColorSet;
            chart.PaletteCustomColors = colorSet;
            //图样式 concave/SoftEdge
            series1["PieDrawingStyle"] = "SoftEdge";
            //分割饼图
            //series.Points[0]["Exploded"] = "true";
            // 设置标签风格
            series1["PieLabelStyle"] = "Inside";
            return new ChartResult(chart);
        }
    }
}
